package com.devilvan.betrice.bilibili.controller;

import com.alibaba.fastjson.JSONObject;
import com.devilvan.betrice.bilibili.dto.BilibiliVideoDto;
import com.devilvan.betrice.bilibili.po.BilibiliVideoPo;
import com.devilvan.betrice.bilibili.service.BilibiliVideoService;
import com.devilvan.betrice.bilibili.vo.BilibiliVideoOwnerVo;
import com.devilvan.betrice.bilibili.vo.BilibiliVideoStatVo;
import com.devilvan.betrice.bilibili.vo.BilibiliVideoVo;
import com.devilvan.betrice.common.BetriceResultVo;
import com.devilvan.betrice.request.BetriceHttpBuilder;
import com.devilvan.betrice.request.component.HttpClientComponent;
import com.devilvan.betrice.request.pojo.BetriceHttpClient;
import com.devilvan.betrice.util.UCollection;
import com.devilvan.betrice.util.UEmpty;
import com.devilvan.betrice.util.UString;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Evad.Wu
 * @Description Bilibili-视频信息表 控制器
 * @date 2022-12-22
 */
@RestController
@RequestMapping(value = "bilibiliVideoController")
@Tag(name = "Bilibili-视频信息表 控制器")
public class BilibiliVideoController {
    @Resource(name = "httpClientComponent")
    private HttpClientComponent httpClientComponent;

    @Resource(name = "bilibiliVideoServiceImpl")
    private BilibiliVideoService bilibiliVideoService;

    /**
     * 总请求控制器
     *
     * @return 形式返回
     */
    @Operation(summary = "总请求控制器")
    @GetMapping(value = "doBilibiliVideo")
    public BetriceResultVo<BilibiliVideoPo> doBilibiliVideo() {
        BetriceResultVo<BilibiliVideoPo> betriceResultVo = new BetriceResultVo<>();
        return betriceResultVo;
    }

    /**
     * 采集报文控制器
     *
     * @param aid     视频ID
     * @param url     完整的请求路径
     * @param headers 请求头信息
     * @return 形式返回
     */
    @Operation(summary = "采集报文控制器")
    @GetMapping(value = "doBilibiliVideoCrawler")
    public BetriceResultVo<BilibiliVideoPo> doBilibiliVideoCrawler(
            @RequestParam String aid,
            @RequestParam(required = false) String url,
            @RequestHeader Map<String, String> headers) {
        Map<String, String> subMap = UCollection.subMap(headers, "cookie");
        BetriceHttpBuilder betriceHttpBuilder = BetriceHttpBuilder.create();
        // 如果请求完整，则按照URL进行请求数据
        if (UEmpty.isEmpty(url)) {
            // 否则则按照aid进行拼接生成url
            Map<String, Object> params = new HashMap<>(1);
            params.put("aid", aid);
            String viewUrlPrefix = "https://api.bilibili.com/x/web-interface/view";
            url = UString.generateHttpParamUrl(viewUrlPrefix, params);
        }
        BetriceHttpClient betriceHttpClient = betriceHttpBuilder.setHttpRequest(RequestMethod.GET, url).setHeaders(subMap).build();
        String json = httpClientComponent.doExecute(betriceHttpClient);
        return this.doBilibiliVideoRequest(json);
    }

    /**
     * 解析报文控制器
     *
     * @param request 报文
     * @return 形式返回
     */
    @Operation(summary = "解析报文控制器")
    @GetMapping(value = "doBilibiliVideoRequest")
    public BetriceResultVo<BilibiliVideoPo> doBilibiliVideoRequest(@RequestBody String request) {
        BilibiliVideoVo bilibiliVideoVo = JSONObject.parseObject(request).getObject("data", BilibiliVideoVo.class);
        BilibiliVideoDto bilibiliVideoDto = this.bilibiliVideoVo2Dto(bilibiliVideoVo);
        return bilibiliVideoService.doBilibiliVideoRequest(bilibiliVideoDto);
    }

    /**
     * {@link BilibiliVideoVo} -> {@link BilibiliVideoDto}
     *
     * @param bilibiliVideoVo vo
     * @return dto
     */
    private BilibiliVideoDto bilibiliVideoVo2Dto(BilibiliVideoVo bilibiliVideoVo) {
        BilibiliVideoDto bilibiliVideoDto = new BilibiliVideoDto();
        bilibiliVideoDto.setAid(bilibiliVideoVo.getAid());
        bilibiliVideoDto.setBvid(bilibiliVideoVo.getBvid());
        bilibiliVideoDto.setCid(bilibiliVideoVo.getCid());
        bilibiliVideoDto.setVideoType(bilibiliVideoVo.getVideoType());
        bilibiliVideoDto.setTitle(bilibiliVideoVo.getTitle());
        bilibiliVideoDto.setDescription(bilibiliVideoVo.getDescription());
        bilibiliVideoDto.setCtime(new Date(bilibiliVideoVo.getCtime() * 1000L));
        bilibiliVideoDto.setPubDate(new Date(bilibiliVideoVo.getPubDate() * 1000L));
        BilibiliVideoOwnerVo bilibiliVideoOwnerVo = bilibiliVideoVo.getBilibiliVideoOwnerVo();
        bilibiliVideoDto.setOwnerId(bilibiliVideoOwnerVo.getOwnerId());
        bilibiliVideoDto.setOwnerName(bilibiliVideoOwnerVo.getOwnerName());
        BilibiliVideoStatVo bilibiliVideoStatVo = bilibiliVideoVo.getBilibiliVideoStatVo();
        bilibiliVideoDto.setView(bilibiliVideoStatVo.getView());
        bilibiliVideoDto.setDanmaku(bilibiliVideoStatVo.getDanmaku());
        bilibiliVideoDto.setReply(bilibiliVideoStatVo.getReply());
        bilibiliVideoDto.setVideoLike(bilibiliVideoStatVo.getVideoLike());
        bilibiliVideoDto.setCoin(bilibiliVideoStatVo.getCoin());
        bilibiliVideoDto.setFavorite(bilibiliVideoStatVo.getFavorite());
        bilibiliVideoDto.setShare(bilibiliVideoStatVo.getShare());
        bilibiliVideoDto.setHisRank(bilibiliVideoStatVo.getHisRank());
        return bilibiliVideoDto;
    }
}
